/**
 * Created by ritamashin on 2017/11/8.
 */
import {ApiResultError, ERROR_CODE_DUPLICATED, ERROR_CODE_NOT_FOUND,
    ERROR_CODE_WRONG_PASSWORD, ERROR_CODE_INVALID_VERIFY_CODE} from '../error'
import * as apis from '../apis'
import * as actions from './'

export function getHomepageBannerInfo(channel) {
    return dispatch => {
        apis.getHomePageBanner(channel)
            .then(response => {
                console.log(response);
                let {code, data, message} = response;
                if (code === 200){

                    dispatch(appendHomePageBannerData(data["banners"]))
                }else {
                }
            })
            .catch(error => {
                dispatch(actions.errorFlash(error));
            })
    }
}

export function getRecommendHeaderTitle() {
    return dispatch => {
        let data =
            [{'title':'精选','tagId':101},
            {'title' : '送女友', 'tagId' : 10},
            {'title' : '送同事', 'tagId' : 17},
            {'title' : '送基友', 'tagId' : 26},
            {'title' : '送长辈', 'tagId' : 6}];
        dispatch(appendHomePageRecommendHeaderData(data));

        let tagId = data[0]['tagId'];
        dispatch(getRecommendGiftItemData(tagId,));

    }
}

export function getRecommendGiftItemData(tagId, offset = 0,index = 0) {
    return dispatch => {
        dispatch(actions.setHomepageRefreshing(true));
        apis.getGiftItemData(tagId, offset)
            .then(response =>{
                dispatch(actions.setHomepageRefreshing(false));
                let giftItemsData = response['data']['items'];
                dispatch({
                    type : 'HOMEPAGE_APPEND_GIFT_DATA',
                    giftItemsData,
                    tagId,
                    offset,
                    index,
                })
            })
            .catch(()=>{
                dispatch(actions.setHomepageRefreshing(false));
            })
    }
}

export function setHomepageRefreshing( isLoadMoreData : Boolean) {
    return {
        type : 'HOMEPAGE_SET_REFRESHING',
        isLoadMoreData
    }
}

export function getSecondBannerData(gender = 1, generation = 2) {
    return dispatch => {
        apis.getSecondBannerData(gender, generation)
            .then(response => {
                let {code, data, message} = response;
                dispatch({
                    type : 'HOMEPAGE_APPEND_SECOND_BANNER_DATA',
                    secondBannerData : data["secondary_banners"],
                })
            })
            .catch(error => dispatch(actions.errorFlash(error)))
    }
}



function appendHomePageBannerData(bannerData) {
    return dispatch => {
            dispatch({
                type : 'HOMEPAGE_APPEND_BANNER_DATA',
                bannerData,
            })
    }
}

function appendHomePageRecommendHeaderData(recommendHeaderData) {
    return dispatch => {
        dispatch({
            type: 'HOMEPAGE_APPEND_RECOMMEND_HEADER_DATA',
            recommendHeaderData,
        })
    }
}